import sys
import os.path 

cur_path = os.path.dirname(__file__)
parent_path = os.path.abspath(os.path.join(cur_path, '..'))
sys.path.append(parent_path)

import recommender
import pymongo
import redis 

from exceptions import Exception

def shop_rec(nick, mdb, rdb):
    recommender.shop_rec(nick, mdb, rdb)
    
def testing_shop_rec(nick):
    m = pymongo.Connection(port=57324)
    rdb = redis.StrictRedis(host='127.0.0.1', port=56379, db=11)
    shop_rec(nick, m.taogold, rdb)
    rec = m.taogold.shop_rec.find_one({'_id':nick})
    import pprint
    pprint.pprint(rec)


def batch_rec():
    m = pymongo.Connection(port=57324)
    db = m.taogold
    rdb = redis.StrictRedis(host='127.0.0.1', port=56379, db=11)
    user_cur = db.say_user.find(fields={'_id':True})
    for u in user_cur:
        print u
        try:
            shop_rec(u['_id'], db, rdb)
        except Exception as e:
            print 'error in process: ', u, repr(e)


if __name__ == '__main__':
    #nick = sys.argv[1]
    #testing_shop_rec(nick)
    batch_rec()

